<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <title>${setting.siteName} - 编辑药品表</title>
    <meta name="keywords" content="${setting.siteName}">
    <meta name="description" content="${setting.siteName}">
    <!-- BEGIN HEADER -->
    [#include "/admin/include/style.ftl"]
    <style>
        .imgPath {
            display: block;
        }
    </style>
    <!-- END HEADER -->
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content  animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑药品表</h5>
                    <div class="ibox-tools">
                        <button type="button" class="btn btn-outline btn-white btn-xs" id="btn-return-loippi"
                                onclick="location.href='${base}/admin/medicine/list.jhtml'"><i
                                    class="fa fa-reply-all"></i> 返回列表
                        </button>
                    </div>
                </div>
                <div class="ibox-content">
                    <form id="inputForm" class="form-horizontal" action="${base}/admin/medicine/update.jhtml"
                          method="post">
                        <input type="hidden" value="${tMedicine.id}" name="id"/>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">药品名称</label>
                                    <div class="col-sm-9">
                                        <input type="text" name="name" class="form-control" maxlength="200"
                                               value="${(tMedicine.name)!''}"/>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">分类名称</label>
                                    <div class="col-sm-9">
                                        <input type="hidden" name="stairName" class="form-control" maxlength="200"
                                               value="${(tMedicine.stairId)!''}"/>
                                        <select class="form-control" id="idSelect" name="stairId" maxlength="200"
                                                required="required" onchange="forValue()">
                                            <option>请选择</option>
                                            [#list categories as category]
                                                <option value="${category.id}">${category.name}</option>
                                            [/#list]
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">药品优惠比例</label>
                                    <div class="col-sm-9">
                                        <input type="number" name="preferential" class="form-control" maxlength="200"
                                               value="${(tMedicine.preferential*100)!''}"/>
                                        <span style=" position: absolute; top: 2%; right: 3%; display: table-cell;
		                                background-color: #ffffff;white-space: nowrap; padding: 5px 8px;">%</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--/row-->
                        <div class="form-group">
                            <div class="row">
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">药品价格</label>
                                    <div class="col-sm-9">
                                        <input type="number" name="price" class="form-control" maxlength="200"
                                               value="${(tMedicine.price)!''}"/>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">药品原价</label>
                                    <div class="col-sm-9">
                                        <input type="number" name="maxLinePrice" class="form-control" maxlength="200"
                                               value="${(tMedicine.maxLinePrice)!''}"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                [#--                       			<div class="col-sm-6">--]
                                [#--                       			 	<label class="col-sm-3 control-label">审核状态</label>--]
                                [#--	                                <div class="col-sm-9">--]
                                [#--	                                    <input type="text" name="type" class="form-control" maxlength="200" value="${(tMedicine.type)!''}"/>--]
                                [#--	                                </div>--]
                                [#--                       			</div>--]
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">库存</label>
                                    <div class="col-sm-9">
                                        <input type="text" name="repertory" class="form-control" maxlength="200"
                                               value="${(tMedicine.repertory)!''}"/>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">药品简介</label>
                                    <div class="col-sm-9">
                                        <input type="text" name="medicineDesc" class="form-control" maxlength="200"
                                               value="${(tMedicine.medicineDesc)!''}"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--/row-->

                        <div class="form-group">
                            <div class="row">
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">售卖状态</label>
                                    <div class="col-sm-9">
                                        [#--	                                    <input type="text" name="status" class="form-control" maxlength="200" value="${(tMedicine.status)!''}"/>--]
                                        <input type="radio" name="status" value="1"/>上架
                                        <input type="radio" name="status" value="2"/>下架
                                    </div>
                                </div>
                                [#--                       			<div class="col-sm-6">--]
                                [#--                       			 	<label class="col-sm-3 control-label">药品缩略图</label>--]
                                [#--	                                <div class="col-sm-9">--]
                                [#--	                                    <input type="text" name="thumbnailPic" class="form-control" maxlength="200" value="${(tMedicine.thumbnailPic)!''}"/>--]

                                [#--	                                </div>--]
                                [#--                       			</div>--]
                            </div>
                        </div>
                        <!--/row-->

                        <div class="form-group">
                            <div class="row">
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label">图文详情</label>
                                    <div class="col-sm-9">
                                        [#--	                                    <input type="text" name="productDetails" class="form-control" maxlength="200" value="${(tMedicine.productDetails)!''}"/>--]
                                        <textarea id="editor" name="productDetails"
                                                  class="editor">${(tMedicine.productDetails)!''}</textarea>
                                    </div>
                                </div>
                                <div class="col-sm-6">
                                    <label class="col-sm-3 control-label"><em style="color: red;">*&nbsp;</em>药品缩略图<br/></label>
                                    <div class="col-sm-9">
                                        <img id="imgPath" class="imgPath" src="${(tMedicine.thumbnailPic)!''}"
                                             style="width:200px;height:300px"/>
                                        <input type="file" name="file" class="form-control upload"
                                               onchange="change(this)" maxlength="200"/>
                                        <input type="button" class="btn btn-warning uploadBtn" value="修改"
                                               style="margin-top: 300px"/>
                                        <input type="hidden" name="thumbnailPic" class="form-control imagePath"
                                               maxlength="200"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--/row-->
                        <!--药品轮播图管理-->
                        <div class="form-group">
                            <div class="row">
                                <div class="col-sm-12">
                                    <label class="col-sm-3 control-label">药品轮播图片<p style="color: red">
                                            (宽500*高500像素，大小在120KB以内，最多5张，要求JPG或GIF格式)</p></label>
                                    <input type="hidden" name="images" id="images">
                                    <div class="col-sm-9" id="pictureList">
                                    </div>
                                    [#-- <div class="col-sm-9" id="previewImages"></div>--]
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-12 col-sm-offset-10">
                                <button type="button" class="btn btn-default" onclick="window.history.back();">返回
                                </button>
                                <button class="btn btn-success" type="submit">保存内容</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- BEGIN Script -->
[#include "/admin/include/script.ftl"]
<!-- END Script -->
<script>
    /*多图片*/
    var picList = $("#pictureList").zupImg({
        "targetId": "images",
        "maxFileSize": 10,
        "preview": false,
        "mul": true
    });

    //加载多图片
    function loadImgs() {
        var imgStrs = "${tMedicine.picList}";
        if (imgStrs != '') {
            var arr = imgStrs.split(',');
            for (var i in arr) {
                if(arr[i].startsWith("[")){
                    arr[i]=arr[i].substring(1);
                }
                if(arr[i].endWith("]")){
                    arr[i]=arr[i].substr(0,arr[i].length-1);
                }
                picList.addImg(arr[i]);
            }
        }
    }

    $(document).ready(function () {
        [@flash_message /]
        $(".chosen-select").chosen({width: "100%"});
        $(".input-daterange").datepicker({keyboardNavigation: false, forceParse: false, autoclose: true});

        $(".browserButton").browser({
            callback: function (url) {
                $("#" + $(this).attr('for')).val(url);
            }
        });

        $(".input-group.date").datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            autoclose: true
        });
        $("#inputForm").validate({
            submitHandler: function (form) {
                //图片
                $.each(picList.getImgs(), function (index) {
                    $(form).append("<input type='hidden' value='" + this + "' name='picList'/>");
                });
                form.submit();
                console()
            },
            rules: {
                name: {
                    required: true
                },
                medicineDesc: {
                    required: true
                },
                stairName: {
                    required: true
                },
                repertory: {
                    required: true
                },
                avatarUrl: {
                    required: true
                },
                price: {
                    required: true
                },
                productDetails: {
                    required: true
                }
            },
            messages: {}
        });
        loadImgs();
    });
    var statuss = document.getElementsByName("status");
    for (var i = 0; i < statuss.length; i++) {
        if (statuss[i].value == ${tMedicine.status}) {
            statuss[i].checked = 'checked'
        }
    }
    // 设置编辑前药品分类名称为默认显示
    $("#idSelect").val("${tMedicine.stairId}");

    // 上传
    function change(obj) {
        var that = obj;
        var formData = new FormData();
        var fileObj = $(obj)[0].files[0];
        formData.append("file", fileObj);
        $.ajax({
            url: '${base}/admin/file/upload.jhtml',
            type: 'POST',
            data: formData,
            dataType: "json",
            async: false,
            processData: false,
            contentType: false,
            success: function (data) {
                $(that).parent().find(".imgPath").show();
                $(that).parent().find(".imgPath").attr("src", data.url);
                $(that).parent().find(".uploadBtn").css({"top": "100px"});
                $(that).parent().find(".imagePath").val(data.url);
                $(that).parent().find(".uploadBtn").val("修改");
            }
        })
    }

    function forValue() {
        var stairName = $("#idSelect").find("option:selected").text();
        $("input[name='stairName']").val(stairName);
    }

    String.prototype.endWith=function(oString){
        var   reg=new   RegExp(oString+"$");
        return   reg.test(this);
    }
</script>
</body>
</html>
